﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Invoicing.Model.Views;
namespace Invoicing.Command.Views
{
    public class ViewProductOrderCommand
    {
        public static ViewProductOrderCollection Gets(ViewProductOrderQuery sea)
        {
            var ret = new ViewProductOrderCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<ViewProductOrder> q = db.DataContext.ViewProductOrders;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.MaterialID > 0)
                        q = q.Where(p => p.MaterialID == sea.MaterialID);
                    if (sea.ProcessID > 0)
                        q = q.Where(p => p.ProcessID == sea.ProcessID);
                    if (!string.IsNullOrEmpty(sea.Size))
                        q = q.Where(p => p.Size == sea.Size);
                    if (!string.IsNullOrEmpty(sea.OrderNo))
                        q = q.Where(p => p.OrderNo == sea.OrderNo);
                    if (sea.PType > 0)
                        q = q.Where(p => p.PType == sea.PType);
                    if (sea.OrderDateBefore != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.OrderDateBefore.Value) >= 0);
                    if (sea.OrderDateAfter != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.OrderDateAfter.Value) <= 0);
                    if (sea.Method != null && sea.Method.Length > 0)
                        q = q.Where(p => sea.Method.Contains(p.Method));
                    if (sea.HouseID > 0)
                        q = q.Where(p => p.HouseID == sea.HouseID);
                }
                var n = q.Count();
                if (n > 0)
                {

                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<ViewProductOrderModel, ViewProductOrder, ViewProductOrderCollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }
    }
}
